<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>导入数据管理</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            导入数据管理
                        </div>
                        <div class="layui-card-body">
                            <div class="layui-row">
                                <select id="status" style="position: relative;float: right;" onchange="reloadList()">
                                    <option value="1">启用的</option>
                                    <option value="0">禁用的</option>
                                    <option value="-1">全部</option>
                                </select>&nbsp;&nbsp;
                                <a href="add.html" class="layui-btn layui-btn-sm layui-btn-radius"><i class="layui-icon layui-icon-add-1"></i>导入数据</a>
                            </div>
                            <table class="layui-hide" id="LAY-customer-source-manage" lay-filter="LAY-customer-source-manage"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        var reloadList;
        layui.use(['form', 'table'], function(){
            var $ = layui.$
                ,form = layui.form
                ,layer = layui.layer
                ,table = layui.table;

            var status = $('#status').find("option:selected").val();
            table.render({
                elem: '#LAY-customer-source-manage'
//            ,height: 'full-200'
                ,url: '/customer_source/list_data' //数据接口
                ,where: {//设定异步数据接口的额外参数
                    status: status
                }
                ,page: true //开启分页
                ,cols: [[ //表头
//                {field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
                    {field: 'created', title: '添加时间', width: 170}
                    ,{field: 'name', title: '名称', width:200}
//                ,{field: 'type_id', title: '数据类型', width:100}
                    ,{field: 'overview', title: '概览', templet: '#overviewTpl'}
                    ,{field: 'msg', title: '提示信息', width:140}
                    ,{field: 'status', title: '状态', width: 100, templet: '#statusTpl'}
                    , {fixed: 'right', width: 90, align: 'center', toolbar: '#barDemo'}
                ]]
            });

            //监听工具条
            table.on('tool(LAY-customer-source-manage)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                if(layEvent === 'detail'){ //查看
                    //do somehing
                } else if(layEvent === 'del'){ //删除
                    layer.confirm('真的删除行么', function(index){

                    });
                } else if(layEvent === 'edit'){ //编辑
                    //do something

                }
            });
            //监听状态操作
            form.on('switch(statusDemo)', function(obj){
                var status = 0;
                if (obj.elem.checked) {
                    status = 1;
                }
//            layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                var id = this.step;
//            console.log(id);
                var post = {
                    id: id,
                    status: status
                };
                $.ajax({
                    type: "POST",
                    url: "/customer_source/updateStatus",
                    data: post,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
                            layer.tips('修改成功', obj.othis);
                        } else if(dataObj.code == -1) {
                            layer.tips(dataObj.msg, obj.othis);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }

                        setTimeout(function(){
                            reloadList();
                        }, 1000);
                    },
                    error: function (request, status, error) {

                    }
                });
                return false;
            });

            reloadList = function () {
                var status = $('#status').find("option:selected").val();
                table.reload('LAY-customer-source-manage', {
                    where: { //设定异步数据接口的额外参数，任意设
                        status: status
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            };
        });

    </script>
    <script type="text/html" id="overviewTpl">
        {{# if(d.file_import_status == 0){ }}
        <span style="color: blue;">等待导入</span>
        {{# } else if(d.file_import_status == 1){ }}
        <span style="color: orange;">正在导入</span>
        {{# } else if(d.file_import_status == 2){ }}
        共<span style="color: purple;">{{ (d.total>0 ? d.total : d.overview.total) }}</span>，待加<span style="color: blue;">{{d.overview.waitingAdd}}</span>，已加<span style="color: orange;">{{d.overview.requested+d.overview.agreed}}</span>(通过<span style="color: green;">{{d.overview.agreed}}</span>，待通过{{d.overview.requested}})，无效<span style="color: gray;">{{d.overview.notWx}}</span>，失败<span style="color: red;">{{d.overview.failed}}</span>
        {{# } else if(d.file_import_status == 3){ }}
        <span style="color: red;">导入失败</span>
        {{# } }}
    </script>
    <script type="text/html" id="statusTpl">
        <input type="checkbox"  name="status" step="{{d.id}}" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="statusDemo" {{ d.status == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="barDemo">
        <!--    <a class="layui-btn layui-btn-xs layui-btn-normal" href="/customer_source/view/{{ d.id }}">查看</a>-->
        {{# var curTime = new Date('2019-01-22'); }}
        {{# var created = d.created;console.log(created);console.log(created); }}
        {{# var createdTime = new Date(created.substr(0, 10)); }}
        {{# if(d.file_import_status == 2 && createdTime.getTime()>curTime.getTime()){ }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" href="/customer_source/downloadNotWeChat/{{ d.id }}" target="_blank">无效数据</a>
        {{# } }}
    </script>
</div>
</body>
</html>
